// subpackagesA/pages/dailyReport/dailyReport.ts
import { RoomRanking, FindTop20, FindLatter20, HRZData, RYZData, getHeat, getRYZ, searchHeat, searchRYZ } from '../../../api/daily'
import { findAllInfo, MetervillageServer } from '../../../api/room-monitoring'
const dayjs = require('../../../utils/day.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comID: 0,
    date: dayjs().subtract(1, 'day').format('YYYY-MM-DD'),
    teDay: dayjs().subtract(1, 'day').format('MM月DD日'),
    nextDay: dayjs().subtract(1, 'day').format('YYYY-MM-DD'),
    isDisabled: true,
    item: [
      {
        title: '室温采集报表',
        color: '#03C678',
        id: 1,
      },
      {
        title: '换热站报表',
        color: '#F23B1C',
        id: 2
      },
      {
        title: '热源站报表',
        color: '#8C1FD2',
        id: 3
      },
      {
        title: '换热站异常',
        color: '#F23B1C',
        id: 4
      },
      {
        title: '热源站异常',
        color: '#8C1FD2',
        id: 5
      },
      {
        title: '分户计量异常',
        color: '#03C678',
        id: 6
      }
    ],
    itemIndex: 1,
    data: [],
    ListData1: [],
    ListData2: [],
    HRZList: [],
    RYZList: [],
    echartsList1: [],
    echartsList2: [],
    active: 1,
    tableHeader: [
      {
        prop: 'heatExchangeStationName',
        label: "换热站名称",
        width: 240
      },
      {
        prop: '日实际能耗',
        label: "耗热量GJ",
        width: 240
      },
      {
        prop: '日实际单位能耗',
        label: '单位能耗W/㎡',
        width: 240
      },
      {
        prop: '采暖季实际能耗',
        label: '采暖季累计耗热GJ',
        width: 240
      },
      {
        prop: '采暖季实际单位能耗',
        label: '采暖季单位能耗W/㎡',
        width: 240
      },
    ],
    tableHeader2: [
      {
        prop: 'heatSourceName',
        label: "换热站名称",
        width: 240
      },
      {
        prop: '日实际能耗',
        label: "耗热量GJ",
        width: 240
      },
      {
        prop: '日实际单位能耗',
        label: '单位能耗W/㎡',
        width: 240
      },
      {
        prop: '采暖季实际能耗',
        label: '采暖季累计耗热GJ',
        width: 240
      },
      {
        prop: '采暖季实际单位能耗',
        label: '采暖季单位能耗W/㎡',
        width: 240
      },
    ],
    time: false,
    currentDate: new Date().getTime(),
    minDate: new Date(2024, 1, 1).getTime(),
    timeValue: '',
    HRZName: '站点名称',
    HRZid:'',
    villageName: '小区名称',
    villageID: 0,
    showVillage: false,
    showSelect: false,
    columns: [],
    isFresh: false,
    page: 1,
    limit: 5,
    FHData: [],
    selectList: [],
    listData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getData();
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },
  onItem(e: any) {
    const { id } = e.currentTarget.dataset
    this.setData({
      itemIndex: id,
      HRZName:'站点名称',
      listData:[],
    })
    this.getData();
  },
  getData() {
    const date = this.data.date
    const tabIndex = this.data.itemIndex
    const comID = wx.getStorageSync("userInfo").data.comID
    if (tabIndex == 1) {
      RoomRanking(comID, date).then((res: any) => {
        this.setData({
          data: res.data
        })
      });
      FindTop20(comID, date).then((res: any) => {
        this.setData({
          ListData1: res.data
        })
      });
      FindLatter20(comID, date).then((res: any) => {
        this.setData({
          ListData2: res.data
        })
      })
    } else if (tabIndex == 2) {
      HRZData(comID, date).then((res: any) => {

        let list = res.data.slice()
        const total1 = list.reduce((acc, item) => acc + item.日实际能耗, 0).toFixed(2);
        const total2 = list.reduce((acc, item) => acc + item.日实际单位能耗, 0).toFixed(2);
        const total3 = list.reduce((acc, item) => acc + item.采暖季实际能耗, 0).toFixed(2);
        const total4 = list.reduce((acc, item) => acc + item.采暖季实际单位能耗, 0).toFixed(2);
        list.push({
          heatExchangeStationName: "统计",
          日实际能耗: total1,
          日实际单位能耗: total2,
          采暖季实际能耗: total3,
          采暖季实际单位能耗: total4
        })
        this.setData({
          HRZList: list,
          echartsList1: res.data
        })
      })
    } else if (tabIndex == 3) {
      RYZData(comID, date).then((res: any) => {
        let list = res.data.slice()
        const total1 = list.reduce((acc, item) => acc + item.日实际能耗, 0).toFixed(2);
        const total2 = list.reduce((acc, item) => acc + item.日实际单位能耗, 0).toFixed(2);
        const total3 = list.reduce((acc, item) => acc + item.采暖季实际能耗, 0).toFixed(2);
        const total4 = list.reduce((acc, item) => acc + item.采暖季实际单位能耗, 0).toFixed(2);
        list.push({
          heatSourceName: "统计",
          日实际能耗: total1,
          日实际单位能耗: total2,
          采暖季实际能耗: total3,
          采暖季实际单位能耗: total4
        })
        this.setData({
          RYZList: list,
          echartsList2: res.data
        })
      })
    } else if (tabIndex == 4) {
      getHeat(comID).then(res => {
        this.setData({
          selectList: res.data
        })
      })
    }else if(tabIndex == 5){
      getRYZ(comID).then(res=>{
        console.log(res)
        this.setData({
          selectList:res.data
        })
      })
    } else if (tabIndex == 6) {
      this.getVillageData();
    }
  },
  onSearch() {
    const time = this.data.timeValue;
    const tabIndex = this.data.itemIndex
    const comID = wx.getStorageSync("userInfo").data.comID
    const hrzID = this.data.HRZid
    if(tabIndex == 4){
      searchHeat(comID,time,hrzID).then(res=>{
        this.setData({
          listData:res.data
        })
      })
    }
    if(tabIndex==5){
      searchRYZ(comID,time,hrzID).then(res=>{
        this.setData({
          listData:res.data
        })
      })
    }
    if (tabIndex == 6) {
      this.getVillageData();
    }
  },
  LastDay() {
    this.setData({
      date: dayjs(this.data.date).subtract(1, 'day').format('YYYY-MM-DD')
    })
    this.getData();
  },
  NextDay() {
    this.setData({
      date: dayjs(this.data.date).add(1, 'day').format('YYYY-MM-DD')
    })
    this.getData();
  },
  onTime() {
    this.setData({
      time: true
    })
  },
  onVillage() {
    this.setData({
      showVillage: true
    })
  },
  onClose() {
    this.setData({
      time: false,
      showVillage: false,
      showSelect: false
    })
  },
  onSelect() {
    this.setData({
      showSelect: true
    })
  },
  onConfirm(e: any) {
    const time = dayjs(e.detail).format('YYYY-MM-DD')
    this.setData({
      timeValue: time,
      time: false
    })
  },
  onChange(e: any) {
    const tabIndex = this.data.itemIndex
    if (tabIndex == 4) {
        this.setData({
          showSelect:false,
          HRZName:e.detail.value.heatExchangeStationName,
          HRZid:e.detail.value.id
        })
    }else if(tabIndex==5){
      this.setData({
        showSelect:false,
        HRZName:e.detail.value.heatSourceName,
        HRZid:e.detail.value.id
      })
    } else if (tabIndex == 6) {
      const villageID = e.detail.value.villageID;
      this.setData({
        showVillage: false,
        villageName: e.detail.value.villageName,
        villageID: villageID
      })
    }

  },
  getVillageData() {
    const that = this;
    that.setData({
      isFresh: true,
      page: 1
    })
    const comID = wx.getStorageSync("userInfo").data.comID;
    const villageID = that.data.villageID;
    const page = that.data.page;
    const limit = that.data.limit;
    const time = that.data.timeValue;
    findAllInfo(comID, time, villageID, page, limit).then((res: any) => {
      that.setData({
        FHData: res.data.data,
        isFresh: false
      })
    })
  },
  fresh() {
    this.getVillageData();
  },
  onPage() {
    const that = this;
    that.setData({
      page: that.data.page + 1
    });
    const comID = wx.getStorageSync("userInfo").data.comID;
    const villageID = that.data.villageID;
    const page = that.data.page;
    const limit = that.data.limit;
    const time = that.data.timeValue;
    findAllInfo(comID, time, villageID, page, limit).then((res: any) => {
      that.setData({
        FHData: [...that.data.FHData, ...res.data.data],
        isFresh: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const comID = wx.getStorageSync("userInfo").data.comID
    MetervillageServer(comID).then((res: any) => {
      this.setData({ columns: res.data });
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})